import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    //脚手架
    return Scaffold(
      appBar: AppBar(
        title: Text('列表'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
      ),

      body: buildListView1(),
    );
  }

  List<String> _list = List.generate(20, (index) => '初始项目 $index');

  ScrollController _scrollController = ScrollController();

  //
  bool _isLoadingMore = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _scrollController.addListener(() {
      //是否滑动到了底部
      if (_scrollController.position.pixels >=
              _scrollController.position.maxScrollExtent / 3 * 2 &&
          !_isLoadingMore) {
        //加载更多
        _loadingMoreRequest();
      }
    });
  }

  Future<void> _loadingMoreRequest() async {
    if (_isLoadingMore) {
      return;
    }

    _isLoadingMore = true;
    setState(() {});

    await Future.delayed(Duration(seconds: 2));

    _list.addAll(List.generate(10, (index) => '加载更多的数据 $index'));
    _isLoadingMore = false;
    setState(() {});
  }

  //
  Widget buildListView1() {
    return RefreshIndicator(
      displacement: 40, //指示器出现进距离顶部的距离
      color: Colors.red, //进度指示器的颜色
      backgroundColor: Colors.blue, //指示器的背景颜色
      onRefresh: () async {
        //来个延时
        // 模拟网络 数据
        await Future.delayed(Duration(seconds: 2));
        //数组中的数据
        _list = List.generate(20, (index) => '刷新的数据 $index');
        setState(() {});
      },
      child: ListView.builder(
        controller: _scrollController,
        //数据数量
        itemCount: _list.length + 1,
        //每个Item的样式
        itemBuilder: (BuildContext context, int index) {
          if (index == _list.length) {
            if (_isLoadingMore) {
              return Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                    width: 30,
                    height: 30,
                    child: CircularProgressIndicator(),
                  ),
                ],
              );
            }
            //展示加载更多
            return Text('加载更多 ', textAlign: TextAlign.center);
          }
          String str = _list[index];
          return ListTile(
            leading: Icon(Icons.home),
            title: Text(str),
            subtitle: Text('今天 正在写代码中。。。'),
          );
        },
      ),
    );
  }
}
